package com.xbongbong.paas.help.subform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.domain.entity.WorkOrderCcEntity;
import com.xbongbong.saas.enums.subform.WorkOrderCcTeamEnum;
import com.xbongbong.paas.help.subform.abstracts.AbstractUserTeamTransferSubForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 工单抄送团队 es和数据库格式转化
 * @author 魏荣杰
 * @date 2020/8/24 10:47
 * @since
 * @version
 */
@Component
public class WorkOrderCcTeamTransferSubForm extends AbstractUserTeamTransferSubForm {

    private static final Logger LOG = LoggerFactory.getLogger(WorkOrderCcTeamTransferSubForm.class);

    @Override
    public PaasFormDataEntity transferTeamToFormData(UserTeamEntity userTeamEntity) {
        WorkOrderCcEntity workOrderCcEntity = (WorkOrderCcEntity) userTeamEntity;
        PaasFormDataEntity paasFormDataEntity = createPaasFormDataEntityForUserTeam(userTeamEntity);

        JSONObject teamJson = JSONObject.parseObject(JSON.toJSONString(workOrderCcEntity));

        JSONObject data = new JSONObject();

        WorkOrderCcTeamEnum[] workOrderCcTeamEnums = WorkOrderCcTeamEnum.values();
        for (WorkOrderCcTeamEnum workOrderCcTeamEnum : workOrderCcTeamEnums) {
            String dbAttr = workOrderCcTeamEnum.getDbAttr();
            String dataAttr = workOrderCcTeamEnum.getDataAttr();
            Integer fieldType = workOrderCcTeamEnum.getFieldType();
            formatDataForEs(teamJson, data, dbAttr, dataAttr, fieldType);
        }
        paasFormDataEntity.setData(data);

        return paasFormDataEntity;
    }

    @Override
    public UserTeamEntity transferFormDataToTeam(PaasFormDataEntity paasFormDataEntity) {
        JSONObject data = paasFormDataEntity.getData();

        JSONObject dbData = new JSONObject();
        WorkOrderCcTeamEnum[] workOrderCcTeamEnums = WorkOrderCcTeamEnum.values();
        for (WorkOrderCcTeamEnum workOrderCcTeamEnum : workOrderCcTeamEnums) {
            String dbAttr = workOrderCcTeamEnum.getDbAttr();
            String dataAttr = workOrderCcTeamEnum.getDataAttr();
            Integer fieldType = workOrderCcTeamEnum.getFieldType();
            formatDataForDb(data, dbData, dbAttr, dataAttr, fieldType);

        }
        WorkOrderCcEntity workOrderCcEntity = JSON.parseObject(dbData.toJSONString(), WorkOrderCcEntity.class);
        workOrderCcEntity.setAddTime(paasFormDataEntity.getAddTime());
        workOrderCcEntity.setUpdateTime(paasFormDataEntity.getUpdateTime());
        workOrderCcEntity.setCorpid(paasFormDataEntity.getCorpid());
        workOrderCcEntity.setDel(paasFormDataEntity.getDel());
        workOrderCcEntity.setId(paasFormDataEntity.getId());

        return workOrderCcEntity;
    }
}
